package com.solution.wx.my.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductEntity;
import com.solution.admin.product.service.ProductService;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.constants.PublicStatus.SysSymbol;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.utils.DateUtils;
import com.solution.common.utils.string.StringUtil;
import com.solution.wx.product.entity.ProductBrowseRecordEntity;
import com.solution.wx.product.service.ProductBrowseRecordService;

/**
 * 商品浏览记录web控制层
 * 
 * @author xu_cc
 * @date 2017-11-22 15:19:44
 */
@RequestMapping("/wx/browseRecord")
@Controller
public class ProductBrowseRecordController extends BaseController {

	@Autowired
	private ProductBrowseRecordService productBrowseRecordService;
	
	@Autowired
	private ProductService productService;

	/**
	 * 跳转商品浏览记录查询页面
	 * 
	 * @param ProductBrowseRecord
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	 * @date 2017-11-22 15:19:44
	 */
	@RequestMapping("/toProductBrowseRecordPage")
	public String toListProductBrowseRecordPage(HttpServletRequest request, Model model) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userCode", getUserCode());
			paramMap.put("isLogin", isLogin(request) ? 1 : 0);
			paramMap.put("customerLevel", getCustomerLevel());
			paramMap.put("customerId", getCustomerId());
			List<ProductBrowseRecordEntity> recordList = productBrowseRecordService.listProductBrowseRecord(paramMap, null);
			
			//调整商品价格
			List<ProductEntity> proList = new ArrayList<ProductEntity>();
			Map<Long, ProductEntity> proMap = new HashMap<Long, ProductEntity>();
			for (ProductBrowseRecordEntity entity : recordList) {
				ProductEntity product = new ProductEntity();
				product.setId(entity.getProductId());
				product.setBasePrice(entity.getProductPrice());
				proList.add(product);
				proMap.put(entity.getProductId(), product);
			}
			productService.initProductDefaultSpecPrice(proList);
			for (ProductBrowseRecordEntity entity : recordList) {
				entity.setProductPrice(proMap.get(entity.getProductId()).getProductPrice());
			}
			
			model.addAttribute("recordList", recordList);
			model.addAttribute("recordCount", (null != recordList) ? recordList.size() : 0);
			model.addAttribute("today", DateUtils.formatDate(new Date(), null));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "wx/my/record/wx_browseRecord";
	}

	/**
	 * 跳转商品浏览记录查询页面
	 * 
	 * @param ProductBrowseRecord
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	 * @date 2017-11-22 15:19:44
	 */
	@RequestMapping("/toProductBrowseRecordEditPage")
	public String toProductBrowseRecordEditPage(HttpServletRequest request, Model model) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userCode", getUserCode());
			paramMap.put("isLogin", isLogin(request) ? 1 : 0);
			paramMap.put("customerLevel", getCustomerLevel());
			paramMap.put("customerId", getCustomerId());
			List<ProductBrowseRecordEntity> recordList = productBrowseRecordService.listProductBrowseRecord(paramMap, null);
			
			//调整商品价格
			List<ProductEntity> proList = new ArrayList<ProductEntity>();
			Map<Long, ProductEntity> proMap = new HashMap<Long, ProductEntity>();
			for (ProductBrowseRecordEntity entity : recordList) {
				ProductEntity product = new ProductEntity();
				product.setId(entity.getProductId());
				product.setBasePrice(entity.getProductPrice());
				proList.add(product);
				proMap.put(entity.getProductId(), product);
			}
			productService.initProductDefaultSpecPrice(proList);
			for (ProductBrowseRecordEntity entity : recordList) {
				entity.setProductPrice(proMap.get(entity.getProductId()).getProductPrice());
			}
			
			model.addAttribute("recordList", recordList);
			model.addAttribute("recordCount", (null != recordList) ? recordList.size() : 0);
			model.addAttribute("today", DateUtils.formatDate(new Date(), null));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "wx/my/record/wx_browseRecord_edit";
	}

	/**
	 * 删除足迹
	 * 
	 * @param request
	 * @param idArrStr
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月20日 下午3:13:25
	 */
	@RequestMapping("/doDelRecord")
	@ResponseBody
	public BaseReturn doDelRecord(HttpServletRequest request, String idArrStr) {
		BaseReturn result = new BaseReturn();
		try {
			String[] idArr = idArrStr.split(SysSymbol.COMMA);
			for (String idStr : idArr) {
				if (StringUtil.isEmpty(idStr)) {
					continue;
				}
				productBrowseRecordService.deleteById(Long.parseLong(idStr));
			}
			result.setCode(SysCode.SUCCESS);
			result.setMsg("删除足迹成功！");
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("删除足迹异常！");
		}
		return result;
	}

}
